package com.easymin.weixin.service.impl;

import com.easymin.weixin.entity.WxBind;
import com.easymin.weixin.entity.WxBindCriteria;
import com.easymin.weixin.mapper.BaseMapper;
import com.easymin.weixin.mapper.WxBindMapper;
import com.easymin.weixin.service.WxBindService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class DefaultWxBindService extends AbstractBaseService<WxBind, WxBindCriteria, Long> implements WxBindService {
  @Autowired
  private WxBindMapper wxBindMapper;

  @Override
  public BaseMapper<WxBind, WxBindCriteria, Long> getMapper() {
    return wxBindMapper;
  }

  @Override
  public int insert(WxBind wxBind) {
    wxBind.setCreated(new Date());
    wxBind.setUpdated(new Date());
    return super.insert(wxBind);
  }

  @Override
  public int delete(Long id, String appKey) {

    WxBindCriteria criteria = new WxBindCriteria();

    criteria.createCriteria()
            .andIdEqualTo(id)
            .andAppKeyEqualTo(appKey);

    return wxBindMapper.deleteByExample(criteria);
  }

  @Override
  public WxBind findByOpenIdAndAppKey(String openId, String appKey) {

    WxBindCriteria criteria = new WxBindCriteria();

    criteria.createCriteria()
            .andOpenIdEqualTo(openId)
            .andAppKeyEqualTo(appKey);

    List<WxBind> list = wxBindMapper.selectByExample(criteria);

    if (!CollectionUtils.isEmpty(list)) {
      return list.get(0);
    }

    return null;
  }

  @Override
  public WxBind findByPhoneAndAppKey(String phone, String appKey) {

    WxBindCriteria criteria = new WxBindCriteria();
    criteria.createCriteria()
            .andPhoneEqualTo(phone)
            .andAppKeyEqualTo(appKey);

    List<WxBind> list = wxBindMapper.selectByExample(criteria);

    if (!CollectionUtils.isEmpty(list)) {
      return list.get(0);
    }

    return null;
  }

  @Override
  public WxBind findOne(String phone, String openId, String appKey) {
    WxBindCriteria criteria = new WxBindCriteria();
    criteria.createCriteria()
            .andPhoneEqualTo(phone)
            .andOpenIdEqualTo(openId)
            .andAppKeyEqualTo(appKey);

    List<WxBind> list = wxBindMapper.selectByExample(criteria);

    if (!CollectionUtils.isEmpty(list)) {
      return list.get(0);
    }

    return null;
  }

  @Override
  public int deleteByOpenId(String appKey, String openId) {

    WxBindCriteria criteria = new WxBindCriteria();

    criteria.createCriteria()
            .andOpenIdEqualTo(openId)
            .andAppKeyEqualTo(appKey);

    return wxBindMapper.deleteByExample(criteria);
  }

  @Override
  public int deleteByPhone(String appKey, String phone) {

    WxBindCriteria criteria = new WxBindCriteria();

    criteria.createCriteria()
            .andPhoneEqualTo(phone)
            .andAppKeyEqualTo(appKey);

    return wxBindMapper.deleteByExample(criteria);
  }
}
